package com.example.othree.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.othree.common.PageParam;
import com.example.othree.common.R;
import com.example.othree.entity.Drone;
import com.example.othree.entity.Lidar;
import com.example.othree.service.LidarOzoneManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 激光雷达臭氧观测数据管理Controller
 * Created by zr on 2024/10/16.
 */
@RestController
@CrossOrigin
@RequestMapping("/api")
public class LidarOzoneManagementController {
    @Autowired
    private LidarOzoneManagementService lidarOzoneManagementService;

    @PostMapping("/lidar/create")
    @ApiOperation("新增")
    public R create(@RequestBody Lidar lidar){
        return lidarOzoneManagementService.save(lidar) ? R.ok() : R.error();
    }

    @PostMapping("/lidar/saveBatch")
    @ApiOperation("批量新增")
    public R saveBatch(@RequestBody List<Lidar> lidars){
        return lidarOzoneManagementService.saveBatch(lidars) ? R.ok() : R.error();
    }

    @GetMapping("/lidar/list")
    public List<Lidar> list(){
        return lidarOzoneManagementService.list();
    }

    @PostMapping("/lidar/update")
    @ApiOperation("修改")
    public R update(@RequestBody Lidar lidar){
        return lidarOzoneManagementService.updateById(lidar) ? R.ok() : R.error();
    }

    @PostMapping("/lidar/delete/{id}")
    public R delete(@PathVariable Integer id){
        return lidarOzoneManagementService.removeById(id) ? R.ok() : R.error();
    }

    @PostMapping("/lidar/listPage")
    @ApiOperation("查询、分页")
    public R listPage(@RequestBody PageParam pageParam){
        HashMap param = pageParam.getParam();
        String stationName = param.get("stationName").toString();
        String datetime1 = param.get("earliestDatetime").toString();
        String datetime2 = param.get("latestDatetime").toString();

        Page<Lidar> page = new Page();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        LambdaQueryWrapper<Lidar> lambdaQueryWrapper = new LambdaQueryWrapper();

        if(StringUtils.isNotBlank(stationName) && !"null".equals(stationName)){
            lambdaQueryWrapper.eq(Lidar::getStationName, stationName);
        }
        if(StringUtils.isNotBlank(datetime1) && !"null".equals(datetime1) && StringUtils.isNotBlank(datetime1) && !"null".equals(datetime1)){
            if(datetime1.compareTo(datetime2) == 0){
                lambdaQueryWrapper.eq(Lidar::getDatetime, datetime1);
            } else if (datetime1.compareTo(datetime2)>0) {
                return R.error().message("时间区间选择错误！");
            }else{
                lambdaQueryWrapper.ge(Lidar::getDatetime, datetime1);
                lambdaQueryWrapper.le(Lidar::getDatetime, datetime2);
            }
        }

        IPage result = lidarOzoneManagementService.PageList(page, lambdaQueryWrapper);

        return R.ok().data("result", result);
    }

}
